//
//  MyMeetingRoomView.swift
//  GOFM
//
//  Created by siri on 2024/10/21.
//  Copyright © 2024 Glad. All rights reserved.
//

import SwiftUI
import Combine
import SwiftUIRefresh
import Factory

struct MyMeetingRoomView: View {
    @State private var selectedTab: Int = 0
    private var areaArray = ["苏州","青岛","横滨"]
    @State public var selectFloorIndex = 0
    
    // 定义触发器
    final class Triggers: ObservableObject {
        var load = PassthroughSubject<Void, Never>()
        var reload = PassthroughSubject<Void, Never>()
        var select = PassthroughSubject<Void, Never>()
    }
    
    
    @StateObject var viewModel: MyMeetingRoomViewModel
    @StateObject var output: MyMeetingRoomViewModel.Output
    @StateObject var cancelBag: CancelBag
    @StateObject var triggers: Triggers
    
    init(viewModel: MyMeetingRoomViewModel) {
        let triggers = Triggers()
        let cancelBag = CancelBag()
        
        let input = MyMeetingRoomViewModel.Input(
            loadTrigger: triggers.load.eraseToAnyPublisher(),
            reloadTrigger: triggers.reload.eraseToAnyPublisher(),
            selectTrigger: triggers.select.eraseToAnyPublisher()
        )
        
        let output = viewModel.transform(input, cancelBag: cancelBag)
        
        self._viewModel = StateObject(wrappedValue: viewModel)
        self._output = StateObject(wrappedValue: output)
        self._cancelBag = StateObject(wrappedValue: cancelBag)
        self._triggers = StateObject(wrappedValue: triggers)
    }
    
    var body: some View {
        VStack {
            HStack(spacing: 80) {
                ForEach(0..<3) {
                    index in Button(action: {
                        withAnimation {
                            selectedTab = index
                        }
                    }) {
                        Text(areaArray[index])
                            .font(selectedTab == index ? .system(size: 20):.system(size: 15))
                            .foregroundColor(selectedTab == index ? .black:Color(hex: "$959798"))
                    }
                }
            }.frame(width: UIScreen.main.nativeBounds.width,height: 100).background(Color.white)
            HStack {
                FloorView(areaCode: 0)
                Spacer()
                RoomInfoView(infoCode: selectFloorIndex)
            }
            Spacer()
        }.navigationBarTitle("submit", displayMode: .inline)
                    .navigationBarItems(leading: Button(action : {}){
                        Image(systemName: "chevron.left").foregroundColor(.gray)
                    })
        //        VStack(spacing: 0) {
        //            HStack(spacing: 80){
        //                ForEach(0..<3) {
        //                    index  in {
        //                        Button(action:     {
        //                            withAnimation {
        //                                selectedTab = index
        //                            }
        //                        }) {
        //                            Text(areaArray[index])
        //                                .font(selectedTab == index ? .system(size: 20):.system(size: 15))
        //                                .foregroundColor(selectedTab == index ? .black:Color(hex:"#959798" ))
        //                        }
        //                    }
        //
        //                }
        //            }.frame(width:  UIScreen.main.nativeBounds.width,height: 100).background(.white)
        //            //            Floor2View(areaCode: selectedTab)
        //            Spacer()
        //        }.navigationBarTitle("submit", displayMode: .inline)
        //            .navigationBarItems(leading: Button(action : {}){
        //                Image(systemName: "chevron.left").foregroundColor(.gray)
        //            })
        //    }
    }
}
    

struct MyMeetingRoomView_Preview: PreviewProvider {
    static var previews: some View {
        MyMeetingRoomView(viewModel: MyMeetingRoomViewModel(navigationController: UINavigationController()))
    }
}

extension Container {
    func myMeetingRoomView(navigationController: UINavigationController) -> Factory<MyMeetingRoomView> {
        Factory(self) {
            MyMeetingRoomView(viewModel: MyMeetingRoomViewModel(navigationController: navigationController))
        }
    }
}
